﻿package cn.antscript.airCMS.display 
{
	import cn.antscript.airCMS.display.component.ProgressPanel;
	import flash.display.Bitmap;
	import flash.display.Loader;
	import flash.display.NativeWindow;
	import flash.display.NativeWindowInitOptions;
	import flash.display.NativeWindowSystemChrome;
	import flash.display.NativeWindowType;
	import flash.display.Sprite;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.events.MouseEvent;
	import flash.events.ProgressEvent;
	import flash.net.URLRequest;
	
	/**
	 * ...
	 * @author antscript
	 */
	public class ImgViewer extends NativeWindow
	{
		
		private var _container:Sprite;
		private var _loader:Loader;
		private static var _instance:ImgViewer;
		
		public function ImgViewer(privateClass:PrivateClass) 
		{
			var opt:NativeWindowInitOptions = new NativeWindowInitOptions();
			opt.resizable = false;
			opt.type = NativeWindowType.UTILITY;
			opt.systemChrome = NativeWindowSystemChrome.ALTERNATE;
			super(opt);
			this.alwaysInFront = true;
			_loader = new Loader();
			this.stage.addChild(_loader);
			this.stage.align = StageAlign.TOP_LEFT;
			this.stage.scaleMode = StageScaleMode.NO_SCALE;
			_loader.visible = false;
			
			
			addEventListener(Event.CLOSING, onClose);
			
		}
		
		
		
		private function onClose(e:Event):void 
		{
			e.preventDefault();
			
			visible = false;
		}
		
		public static function getInstance():ImgViewer {
			if (_instance == null) {
				_instance = new ImgViewer(new PrivateClass);
			}
			return _instance;
		}
		
		public function show(url:String):void {
			if (_loader.content != null) {
				(_loader.content as Bitmap).bitmapData.dispose();
				_loader.unload();
			}
			
			ProgressPanel.getInstance().show(this.stage);
			
			_loader.visible = false;
			_loader.load(new URLRequest(url));
			_loader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, onPro);
			_loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onCom);
			_loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, onErr);
			
			activate();
		}
		
		private function onPro(e:ProgressEvent):void 
		{
			ProgressPanel.getInstance().setPro(Number(e.bytesLoaded / e.bytesTotal));
		}
		private function onCom(e:Event):void 
		{
			_loader.contentLoaderInfo.removeEventListener(ProgressEvent.PROGRESS, onPro);
			_loader.contentLoaderInfo.removeEventListener(Event.COMPLETE, onCom);
			_loader.contentLoaderInfo.removeEventListener(IOErrorEvent.IO_ERROR, onErr);
			
			_loader.visible = true;
			
		
			
			stage.stageWidth = _loader.content.width;
			stage.stageHeight = _loader.contentLoaderInfo.height;
			
			ProgressPanel.getInstance().hide();
		}		
		private function onErr(e:IOErrorEvent):void 
		{
			_loader.contentLoaderInfo.removeEventListener(ProgressEvent.PROGRESS, onPro);
			_loader.contentLoaderInfo.removeEventListener(Event.COMPLETE, onCom);
			_loader.contentLoaderInfo.removeEventListener(IOErrorEvent.IO_ERROR, onErr);
			
		}
		
		
		public function closeWin():void {
			close();
		}
		
	}

}

class PrivateClass {
	
}